<?php
    /* Script: podcastxml_libs.php
     * Description: Lib's used for podcastxml.php script
     * History:
     * Author               Date            Description
     * Sherif Mansour       5/31/2008       Initial Version
     */

    // Functions and Classes
    function stripJunk ($text) {
        $outText = "";
    // Strip non-text characters
        for ($c=0; $c<strlen($text); $c++) {
            if (ord($text[$c]) >= 32 AND ord($text[$c]) <= 122)
                $outText.=$text[$c];
        }
        return $outText;
    }

    class CMP3File {
        //properties
        var $title;
        var $artist;
        var $album;
        var $year;
        var $comment;
        var $genre;
        
        function getid3 ($file)
        { // read the ID3 tag from an MP3 file
            if (file_exists($file))
            { //after verifying the file exists,
                $id_start = filesize($file) - 128;
                
                $fp = fopen($file, "r");
                fseek($fp, $id_start);
                $tag = fread($fp,3);
                if ($tag == "TAG")
                {
                    $this->title    = stripJunk(trim(fread($fp, 30)));
                    $this->artist    = stripJunk(trim(fread($fp, 30)));
                    $this->album    = stripJunk(trim(fread($fp, 30)));
                    $this->year        = stripJunk(trim(fread($fp, 4)));
                    $this->comment    = stripJunk(trim(fread($fp, 30)));
                    $this->genre    = stripJunk(trim(fread($fp, 1)));
                    fclose($fp);
                    return true;
                }
                else
                { //there is no ID3 tag in the file
                    fclose($fp);
                    return false;
                }
            } else //the file doesn't exist
                return false;
        }
    }

    function getDir($mp3Dir) {  
    // Returns directory as array[file]=date in newest to oldest order
        $dirArray = array();
        $diskdir = "./$mp3Dir/";
        if (is_dir($diskdir)) {
            $dh = opendir($diskdir);
            while (($file = readdir($dh)) != false ) {
                if (filetype($diskdir . $file) == "file" && $file[0]  != ".") {
                    if (stristr($file, ".") == ".mp3") {
                        $ftime = filemtime($mp3Dir."/".$file); 
                        $dirArray[$file] = $ftime;
                    }
                }
            }
            closedir($dh);
        }
        //asort($dirArray); Use this if you want sorted by last modified date
        ksort($dirArray); //use this if you want sorted by the key (IE the filename)
        $dirArray = array_reverse($dirArray);
        return $dirArray;
    }
?>